﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookMS
{
    public partial class admin2 : Form
    {
        BLL.Base table = new BLL.Base();
        Model.book m_binfo = new Model.book();
        public admin2()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取书号
            label2.Text = id + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
        }
        private void admin2_Load(object sender, EventArgs e)
        {
            Table();
            string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取书号
            label2.Text = id + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
        }
        //从数据库读取数据显示在表格控件中
        public void Table()
        {
            //dataGridView1.Rows.Clear();//清空旧数据
            //dataGridView1.DataSource = null;
            dataGridView1.DataSource = table.table();    
        }
        //根据书号显示数据
        public void TableID()
        {
            m_binfo.id = textBox1.Text.ToString();
            if(table.selectById(m_binfo)!=null)
            {
                dataGridView1.DataSource = table.selectById(m_binfo);
            }
            else
            {
                MessageBox.Show("您要查找的图书不存在！");
            }
            //dc.Close();
            //dataBase.DataBaseClose();
        }
        //根据书名显示数据，模糊查询
        public void TableName()
        {
            m_binfo.name = textBox2.Text.ToString();
            if (table.selectByName(m_binfo)!=null)
            {
                dataGridView1.DataSource = table.selectByName(m_binfo);
            }
            else
            {
                MessageBox.Show("您要查找的图书不存在！");
            }
            //dc.Close();
            //dataBase.DataBaseClose();
        }

        private void button1_Click(object sender, EventArgs e)//添加图书
        {
            admin21 admin21 = new admin21();
            admin21.ShowDialog();
            Table();
        }

        private void button3_Click(object sender, EventArgs e)//删除图书
        {
            try
            {
                DialogResult dialogResult = MessageBox.Show("确认删除吗？", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.OK)
                {
                    //string sql = "delete from book where id='{id}'";
                    //DataBase dataBase = new DataBase();
                    m_binfo.id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                    m_binfo.name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                    m_binfo.author = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                    m_binfo.press = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                    m_binfo.number = Convert.ToInt32( dataGridView1.SelectedRows[0].Cells[4].Value.ToString());

                    if (table.delete(m_binfo) > 0)
                    {
                        MessageBox.Show("删除成功！");
                        Table();
                    }
                    else
                    {
                        MessageBox.Show("删除失败！" /*+ sql*/);
                    }
                    //dataBase.DataBaseClose();
                }
            }
            catch
            {
                MessageBox.Show("请先在表格中选中要删除的图书记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void button2_Click(object sender, EventArgs e)//修改图书
        {
            try
            {
                string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                string author = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                string press = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                string number = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                admin22 admin22 = new admin22(id, name, author, press, number);
                admin22.ShowDialog();
                Table();
            }
            catch
            {
                MessageBox.Show("ERROR!");
            }
        }

        private void button5_Click(object sender, EventArgs e)//书号查询
        {
            TableID();
        }

        private void button6_Click(object sender, EventArgs e)//书名查询
        {
            TableName();
        }

        private void button4_Click(object sender, EventArgs e)//刷新
        {
            Table();
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void button7_Click(object sender, EventArgs e)//多行删除
        {
            DialogResult dialogResult = MessageBox.Show("是否要删除所选图书？", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (this.dataGridView1.SelectedRows.Count>0)
            {
                if(dialogResult==DialogResult.OK)
                {                    
                    for (int n=dataGridView1.SelectedRows.Count;n>0;n--)
                    {
                        m_binfo.id = dataGridView1.SelectedRows[n - 1].Cells[0].Value.ToString();
                        table.deleteById(m_binfo);
                    }
                    Table();
                    MessageBox.Show("成功删除图书信息");
                }
            }
            else
            {
                MessageBox.Show("请先在表格中选中要删除的图书记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }           
        }


    }
}
